package com.nettylern.nio;

import java.nio.IntBuffer;

/**
 * @author liliang
 * @date 2019/12/8.
 */
public class UseBuffer {

    public static void main(String[] args) {
        IntBuffer intBuffer = IntBuffer.allocate(10);
        for (int i = 0; i < 5; i++) {
            intBuffer.put(i);
        }

        System.out.println("capacity=" + intBuffer.capacity());
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        intBuffer.flip();
        System.out.println("==========反转后==========");
        System.out.println("capacity=" + intBuffer.capacity());
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        for (int i = 0; i < intBuffer.limit(); i++) {
            System.out.println(intBuffer.get());
        }
        System.out.println("capacity=" + intBuffer.capacity());
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        intBuffer.rewind();
        System.out.println("==========倒带后==========");
        System.out.println("capacity=" + intBuffer.capacity());
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        for (int i = 0; i < intBuffer.limit(); i++) {
            if (i == 2) {
                intBuffer.mark();
            }
            System.out.println(intBuffer.get());
        }
        intBuffer.reset();
        System.out.println("capacity=" + intBuffer.capacity());
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());
        intBuffer.clear();
        System.out.println("======清空之后=======");
        System.out.println("capacity=" + intBuffer.capacity());
        System.out.println("position=" + intBuffer.position());
        System.out.println("limit=" + intBuffer.limit());

    }
}
